package com.example.demo.controller;


import com.example.demo.enity.Bill;
import com.example.demo.enity.Provider;
import com.example.demo.enity.User;
import com.example.demo.service.BillListService;
import com.example.demo.service.ProviderListService;
import com.example.demo.util.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
public class ProviderListOperationController {

    @Autowired
    ProviderListService providerListService;

    @Autowired
    BillListService billListService;

    @GetMapping("/providerList")
    public String providerList(Model model) {

        String username = LoginExitController.username;
        model.addAttribute("username", username);

        List<Provider> providerList = providerListService.getList(null);
        model.addAttribute("providerList", providerList);
        return "providerList";
    }

    @GetMapping("/searchProvider")
    public String searchProvider(Model model, Provider provider) {

        List<Provider> providerList = providerListService.getList(provider);
        model.addAttribute("providerList", providerList);

        String supplierName = provider.getSupplierName();
        model.addAttribute("supplierName", supplierName);

        return "providerList";
    }


    @GetMapping("/toproviderAdd")
    public String toproviderAdd(Model model) {

        String username = LoginExitController.username;
        model.addAttribute("username", username);

        return "providerAdd";
    }


    @GetMapping("/providerAdd")
    public String providerAdd(Provider provider, Model model) {

        String success = "providerList";
        String fail = "providerAdd";

        provider.setCreateTime(new Date());

        String result = Tools.addObject(provider, model, providerListService, success, fail);

        return result;
    }


    @GetMapping("/providerView")
    public String providerView(Integer code, Model model) {
        String username = LoginExitController.username;
        model.addAttribute("username", username);

        Provider provider = providerListService.findById(code);
        model.addAttribute("provider", provider);

        return "providerView";
    }


    @GetMapping("/providerUpdate")
    public String providerUpdate(Integer code, Model model) {
        String username = LoginExitController.username;
        model.addAttribute("username", username);

        Provider provider = providerListService.findById(code);
        model.addAttribute("provider", provider);

        return "providerUpdate";
    }

    @GetMapping("/updateProvider")
    public String update(Provider provider, Model model) {
        provider.setCreateTime(new Date());

        String success = "providerList";
        String fail = "providerUpdate";

        String resultProvider = Tools.updateObject(provider, model, providerListService, success, fail);

        List<Bill> billList = billListService.getList(null);
        for (Bill bill : billList) {
            if (provider.getCode().equals(bill.getPId())) {
                String supplierName = provider.getSupplierName();
                bill.setSupplier(supplierName);

                String resultBill = Tools.updateObject(bill, model, fail, billListService);
                if (resultBill != null) {
                    return fail;
                }
            }
        }
        return resultProvider;
    }

    @GetMapping("/removeProvider")
    public String removeProvider(Integer code, Model model) {
        try {
            providerListService.remove(code);
        } catch (Exception e) {
            String message = "删除失败";
            model.addAttribute("message", message);

            String username = LoginExitController.username;
            model.addAttribute("username", username);

            List<Provider> providerList = providerListService.getList(null);
            model.addAttribute("providerList", providerList);

            return "providerList";
        }
        return "redirect:/providerList";
    }

}
